// pages/hsyuyue/index.js
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    zhengjiandata: ['物业', '车位主', '停车人'],
    zhengjian:'物业'
  },


  //证件类型

  onChangezhengjian(event) {

    this.setData({
      zhengjian: event.detail.value,
      zhengjianpd: false,
      show: false
    });
  },

  oncancelzhengjian() {
    this.setData({
      zhengjianpd: false,
      show: false
    })
  },

  chosezhengjian() {
    this.setData({
      zhengjianpd: true,
      show: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},


  onChangea(event) {
    this.setData({
      name: event.detail
    })
  },

  onChangeb(event) {
    this.setData({
      shenfenzheng: event.detail
    })
  },

  onChangec(event) {
    this.setData({
      phone: event.detail
    })
  },

  onChanged(event) {
    this.setData({
      zhanghao: event.detail
    })
  },
  onChangee(event) {
    this.setData({
      mima: event.detail
    })
  },

  onChangex(event) {
    this.setData({
      cheweihao: event.detail
    })
  },

  onChangez(event) {
    this.setData({
      chepai: event.detail
    })
  },

  onChangedizhi(event) {
    this.setData({
      cheweidizhi: event.detail
    })
  },

  yuyue() {

    const zhengjian = this.data.zhengjian
    const username = this.data.name
    const shenfenzheng = this.data.shenfenzheng
    const zhanghao = this.data.zhanghao
    const mima = this.data.mima
    const phone = this.data.phone

    const cheweihao = this.data.cheweihao
    const cheweidizhi = this.data.cheweidizhi


    const chepai = this.data.chepai
    let d = new Date()
    if (!username) {
      wx.showToast({
        title: '请输入名字',
        icon: 'none'
      })
    } else if (!zhengjian) {
      wx.showToast({
        title: '请选择类型',
        icon: 'none'
      })
    } else if (!shenfenzheng) {
      wx.showToast({
        title: '请输入编号',
        icon: 'none'
      })
    } else if (!phone) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    } else if (!zhanghao) {
      wx.showToast({
        title: '请输入账号',
        icon: 'none'
      })
    } 
    else if (zhengjian=='车位主'&&!cheweihao) {
      wx.showToast({
        title: '请输入车位号',
        icon: 'none'
      })
    } 
    else if (zhengjian=='车位主'&&!cheweidizhi) {
      wx.showToast({
        title: '请输入车位地址',
        icon: 'none'
      })
    } 
    else if (zhengjian=='停车人'&&!chepai) {
      wx.showToast({
        title: '请输入车牌号',
        icon: 'none'
      })
    } 
    
    
    else if (!mima) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '注册中',
      })
      db.collection('users').where({
        uid:zhanghao,    
      }).count().then(res => {
        wx.hideLoading({
          success: (res) => {},
        })
        console.log(res.total)
        if (res.total != 0) {
          wx.showModal({
            title: '提示',
            content: '用户已存在',
          })
        } else {
          db.collection('users').add({
            data: {
              cheweidizhi,
              zhengjian,
              username,
              shenfenzheng,
              uid:zhanghao,
              password:mima,
              phone,
              cheweihao,
              ischeweihao:!!cheweihao,
              chepai,
              ischepai:!!chepai,
              yuyue:false,
              time: d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(),
            }
          }).then(res => {
            console.log(res);
   
            if (res._id) {
    
              wx.showToast({
                title: '注册成功',
              })
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        }
      }
        )




    }
  }
})